import qs from 'qs';
import axios from 'axios';
import { blobResResolve, reqResolveByEncrypt, resResolve, resReject, reqResolve, reqReject } from './modules/interceptors';

// 基础的api地址
const baseApiURL = import.meta.env.VITE_API_URL;
/**
 * 创建axios对象
 *
 * @param {Object} params { isBlob:是否为blob响应, isEncrypt:是否为请求加密 }
 * @param {Object} options 一些配置
 * @returns axios
 */
export function createAxios({ isBlob = false, isEncrypt = true, baseURL='' } = {}, options) {
	const server = axios.create({
		baseURL: baseURL || baseApiURL,
		// `timeout` 指定请求超时的毫秒数。30分钟
		timeout: 1000 * 60 * 10,
		paramsSerializer: {
			serialize(params) {
				return qs.stringify(params, { allowDots: true });
			},
		},
		...options,
	});
	// 请求拦截-通用拦截
	server.interceptors.request.use(reqResolve, reqReject);

	// 请求拦截-加密拦截
	if (isEncrypt) {
		server.interceptors.request.use(reqResolveByEncrypt, reqReject);
	}

	// 响应拦截
	if (isBlob) {
		server.interceptors.response.use(blobResResolve, resReject);
	} else {
		server.interceptors.response.use(resResolve, resReject);
	}
	return server;
}
// 默认axios
export const defAxios = createAxios();
// blob方式axios
export const blobAxios = createAxios({ isBlob: true });
// 请求参数加密的axios
export const encryptAxios = createAxios({ isEncrypt: true });

// mook数据axios
export const apiFoxAxios = createAxios({ baseURL: '/foxapi' })
// bpmn调试axios
export const apiBpmnAxios = createAxios({ baseURL: '/bpmnapi' })
